package com.css.fxfzaqbz.modules.assess.service.impl;

import com.css.fxfzaqbz.base.upload.entity.AttachmentInfoEntity;
import com.css.fxfzaqbz.base.upload.service.AttachmentInfoService;
import com.css.fxfzaqbz.constants.YNEnum;
import com.css.fxfzaqbz.modules.assess.entity.*;
import com.css.fxfzaqbz.modules.assess.repository.SecondDisastersRepository;
import com.css.fxfzaqbz.modules.assess.service.SecondDisastersService;
import com.css.fxfzaqbz.util.*;
import com.css.fxfzaqbz.web.PlatformSessionContext;
import com.css.fxfzaqbz.zcpt.sys.entity.SUser;
import com.css.fxfzaqbz.zcpt.sys.service.SUserService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.*;
import java.sql.Timestamp;
import java.text.NumberFormat;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @author y1fan
 * @create 2022-09-14 18:06
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class SecondDisastersServiceImpl implements SecondDisastersService {


    @Resource
    private SecondDisastersRepository secondDisastersRepository;

    @Resource
    private SUserService sUserService;

    @Resource
    private AttachmentInfoService attachmentInfoService;

    @Value("${image.localDir}")
    private String localDir;

    @Override
    public SecondDisastersEntity saveSecondDisasters(SecondDisastersEntity secondDisastersEntity)throws IOException {

        secondDisastersEntity.setSsdDate(PlatformDateUtils.getCurrentTimestamp());
        secondDisastersEntity.setSsdId(UUIDGenerator.getUUID());
        secondDisastersEntity.setSsdDelFlag(YNEnum.N.toString());
        secondDisastersEntity.setSsdCreatetime(PlatformDateUtils.getCurrentTimestamp());
        String userID = PlatformSessionContext.getUserID();
        secondDisastersEntity.setSsdCreateuser(userID);
        secondDisastersEntity.setTypes("次生灾害隐患评估");
        secondDisastersEntity.setMark(YNEnum.N.toString());
        if (secondDisastersEntity.getSsdContent().equals("地质灾害统计")) {
            MultipartFile areaImage = Base64DecodeMultipartFile.base64Convert(secondDisastersEntity.getAreaImage());
            MultipartFile monthImage = Base64DecodeMultipartFile.base64Convert(secondDisastersEntity.getMonthImage());
            String areaImageFileId = getImageFileId(areaImage);
            String monthImageFileId = getImageFileId(monthImage);
            secondDisastersEntity.setAreaImage(areaImageFileId);
            secondDisastersEntity.setMonthImage(monthImageFileId);
            secondDisastersEntity.setSsdImageTitle("地质灾害统计");
            int a = secondDisastersRepository.getNumber(secondDisastersEntity);
            if (a!=0) {
                Timestamp currentTimestamp = PlatformDateUtils.getCurrentTimestamp();
                secondDisastersRepository.deleteTJ(secondDisastersEntity.getSsdRelation(),currentTimestamp,userID);
            }
        }
        String userJson = sUserService.getSUser(userID);
        SUser sUser = CreateGson.createGson().fromJson(userJson, SUser.class);
        if (sUser != null) {
            secondDisastersEntity.setSsdPeople(sUser.getUserName());
        }
        return secondDisastersRepository.saveSecondDisasters(secondDisastersEntity);
    }


    @Override
    public List<SecondDisastersEntity> getAllSecondDisasters(String id) {
        return secondDisastersRepository.getAllSecondDisasters(id);
    }

    @Override
    public SecondDisastersEntity getSecondDisastersById(String id) {
        SecondDisastersEntity secondDisastersEntity = secondDisastersRepository.getSecondDisastersById(id);
        if (!PlatformObjectUtils.isEmpty(secondDisastersEntity.getSsdDescribe())) {
            List<String> list = dataString(secondDisastersEntity.getSsdDescribe());
            secondDisastersEntity.setDescribe(list);
        }
        return secondDisastersEntity;

    }

    @Override
    public void deleteSecondDisasters(String id) {
        Timestamp currentTimestamp = PlatformDateUtils.getCurrentTimestamp();
        String userID = PlatformSessionContext.getUserID();

        secondDisastersRepository.deleteSecondDisasters(id, currentTimestamp, userID);
    }

    @Override
    public SecondDisastersEntity getSecondDisastersByContent(String ssdContent, String ssdRelation) {
         return secondDisastersRepository.getSecondDisastersByContentAndIaId(ssdContent, ssdRelation);


    }

    @Override
    public SecondDisastersEntity updateSecondDisastersById(SecondDisastersEntity secondDisastersEntity, String ssdId)throws IOException {


        secondDisastersEntity.setSsdDate(PlatformDateUtils.getCurrentTimestamp());
        String userID = PlatformSessionContext.getUserID();
        secondDisastersEntity.setSsdUpdateuser(userID);
        secondDisastersEntity.setSsdUpdatetime(PlatformDateUtils.getCurrentTimestamp());
        return secondDisastersRepository.updateSecondDisastersById(secondDisastersEntity, ssdId);
    }

    @Override
    public SecondDisastersEntity updateSecondDisasters(SecondDisastersEntity secondDisastersEntity) throws IOException{
        MultipartFile areaImage = Base64DecodeMultipartFile.base64Convert(secondDisastersEntity.getAreaImage());
        MultipartFile monthImage = Base64DecodeMultipartFile.base64Convert(secondDisastersEntity.getMonthImage());
        String areaImageFileId = getImageFileId(areaImage);
        String monthImageFileId = getImageFileId(monthImage);
        secondDisastersEntity.setAreaImage(areaImageFileId);
        secondDisastersEntity.setMonthImage(monthImageFileId);

        secondDisastersEntity.setSsdUpdatetime(PlatformDateUtils.getCurrentTimestamp());
        secondDisastersEntity.setSsdDate(PlatformDateUtils.getCurrentTimestamp());
        String userID = PlatformSessionContext.getUserID();
        secondDisastersEntity.setSsdUpdateuser(userID);
        return secondDisastersRepository.updateSecondDisasters(secondDisastersEntity);
    }

    @Override
    public Map<String, Object> getExcelByMouth(String id) throws Exception {


        AttachmentInfoEntity attach = attachmentInfoService.getAttach(id);
        File file = new File(attach.getFileDirId() + "\\" + attach.getAttachPath());
        //File file=new File("D:\\static\\2022\\09\\21\\按月统计模板_1663741565243.xlsx");
        FileInputStream fileInputStream = new FileInputStream(file);

        // list = ExcelEntityByMouth(fileInputStream);

        //获取横行数据
        List<ByMouthEntity> testEntities = ExcelReaderUtil.readExcel2Bean(fileInputStream, ByMouthEntity.class, 1);
        List<String> mouthList = new ArrayList<>();
        List<Integer> countList = new ArrayList<>();

        List<Integer> zongshuList = new ArrayList<>();
        List<Integer> huapoList = new ArrayList<>();
        List<Integer> bengtaList = new ArrayList<>();
        List<Integer> nishiliuList = new ArrayList<>();
        List<Integer> dimiantaxianList = new ArrayList<>();
        List<Integer> dimianchenjiangList = new ArrayList<>();
        List<Integer> xiepoList = new ArrayList<>();
        List<Integer> qitaList = new ArrayList<>();

        List<List> lists = new ArrayList<>();

        int huapoSum = 0;
        int bengtaSum = 0;
        int nishiliuSum = 0;
        int dimiantaxianSum = 0;
        int diminachenjiangSum = 0;
        int xiepoSum = 0;
        int qitaSum = 0;
        int zongSum = 0;

        for (ByMouthEntity byMouthEntity : testEntities) {
            mouthList.add(byMouthEntity.getMouth());
            //判断各属性不能为空 为空赋值为0
            if (byMouthEntity.getHuapo() == null) {
                byMouthEntity.setHuapo(0);
            }
            if (byMouthEntity.getBengta() == null) {
                byMouthEntity.setBengta(0);
            }
            if (byMouthEntity.getNishiliu() == null) {
                byMouthEntity.setNishiliu(0);
            }
            if (byMouthEntity.getDimiantaxian() == null) {
                byMouthEntity.setDimiantaxian(0);
            }
            if (byMouthEntity.getDimianchenjiang() == null) {
                byMouthEntity.setDimianchenjiang(0);
            }
            if (byMouthEntity.getXiepo() == null) {
                byMouthEntity.setXiepo(0);
            }
            if (byMouthEntity.getQita() == null) {
                byMouthEntity.setQita(0);
            }
            Integer zongshu = byMouthEntity.getHuapo() + byMouthEntity.getBengta() + byMouthEntity.getNishiliu() + byMouthEntity.getDimiantaxian() + byMouthEntity.getDimianchenjiang() + byMouthEntity.getXiepo() + byMouthEntity.getQita();
            countList.add(zongshu);
            byMouthEntity.setZongshu(zongshu);
            //获取每个月的灾害总数
            zongshuList.add(zongshu);
            huapoList.add(byMouthEntity.getHuapo());
            bengtaList.add(byMouthEntity.getBengta());
            nishiliuList.add(byMouthEntity.getNishiliu());
            dimiantaxianList.add(byMouthEntity.getDimiantaxian());
            dimianchenjiangList.add(byMouthEntity.getDimianchenjiang());
            xiepoList.add(byMouthEntity.getXiepo());
            qitaList.add(byMouthEntity.getQita());


        }

        for (Integer integer : huapoList) {
            huapoSum += integer;
        }

        for (Integer integer : bengtaList) {
            bengtaSum += integer;
        }
        for (Integer integer : nishiliuList) {
            nishiliuSum += integer;
        }
        for (Integer integer : dimiantaxianList) {
            dimiantaxianSum += integer;
        }
        for (Integer integer : dimianchenjiangList) {
            diminachenjiangSum += integer;
        }
        for (Integer integer : xiepoList) {
            xiepoSum += integer;
        }
        for (Integer integer : qitaList) {
            qitaSum += integer;
        }

        for (ByMouthEntity byMouthEntity : testEntities) {
            // 创建一个数值格式化对象
            NumberFormat numberFormat = NumberFormat.getInstance();
            // 设置精确到小数点后2位
            numberFormat.setMaximumFractionDigits(1);

            if (huapoSum == 0) {
                huapoSum = 1;
                String result1 = numberFormat.format((float) byMouthEntity.getHuapo() / (float) huapoSum * 100);
                String huapoPP = result1 + "%";
                byMouthEntity.setHuapoP(huapoPP);
                huapoSum = 0;
            } else {
                String result1 = numberFormat.format((float) byMouthEntity.getHuapo() / (float) huapoSum * 100);
                String huapoPP = result1 + "%";
                byMouthEntity.setHuapoP(huapoPP);

            }


            if (bengtaSum == 0) {
                bengtaSum = 1;
                String result1 = numberFormat.format((float) byMouthEntity.getBengta() / (float) bengtaSum * 100);
                String bengtaP = result1 + "%";
                byMouthEntity.setBengtaP(bengtaP);
                bengtaSum = 0;
            } else {
                String result1 = numberFormat.format((float) byMouthEntity.getBengta() / (float) bengtaSum * 100);
                String bengtaP = result1 + "%";
                byMouthEntity.setBengtaP(bengtaP);

            }


            if (nishiliuSum == 0) {
                nishiliuSum = 1;
                String result3 = numberFormat.format((float) byMouthEntity.getNishiliu() / (float) nishiliuSum * 100);
                String nishiliuP = result3 + "%";
                byMouthEntity.setNishiliuP(nishiliuP);
                nishiliuSum = 0;
            } else {
                String result3 = numberFormat.format((float) byMouthEntity.getNishiliu() / (float) nishiliuSum * 100);
                String nishiliuP = result3 + "%";
                byMouthEntity.setNishiliuP(nishiliuP);
            }


            if (dimiantaxianSum == 0) {
                dimiantaxianSum = 1;
                String result4 = numberFormat.format((float) byMouthEntity.getDimiantaxian() / (float) dimiantaxianSum * 100);
                String dimiantaxianP = result4 + "%";
                byMouthEntity.setDimiantaxianP(dimiantaxianP);
                dimiantaxianSum = 0;
            } else {
                String result4 = numberFormat.format((float) byMouthEntity.getDimiantaxian() / (float) dimiantaxianSum * 100);
                String dimiantaxianP = result4 + "%";
                byMouthEntity.setDimiantaxianP(dimiantaxianP);
            }

            if (diminachenjiangSum == 0) {
                diminachenjiangSum = 1;
                String result5 = numberFormat.format((float) byMouthEntity.getDimianchenjiang() / (float) diminachenjiangSum * 100);
                String dimianchenjiangP = result5 + "%";
                byMouthEntity.setDimianchenjiangP(dimianchenjiangP);
                diminachenjiangSum = 0;
            } else {
                String result5 = numberFormat.format((float) byMouthEntity.getDimianchenjiang() / (float) diminachenjiangSum * 100);
                String dimianchenjiangP = result5 + "%";
                byMouthEntity.setDimianchenjiangP(dimianchenjiangP);
            }


            if (xiepoSum == 0) {
                xiepoSum = 1;
                String result6 = numberFormat.format((float) byMouthEntity.getXiepo() / (float) xiepoSum * 100);
                String xiepoP = result6 + "%";
                byMouthEntity.setXiepoP(xiepoP);
                xiepoSum = 0;
            } else {
                String result6 = numberFormat.format((float) byMouthEntity.getXiepo() / (float) xiepoSum * 100);
                String xiepoP = result6 + "%";
                byMouthEntity.setXiepoP(xiepoP);
            }

            if (qitaSum == 0) {
                qitaSum = 1;
                String result7 = numberFormat.format((float) byMouthEntity.getQita() / (float) qitaSum * 100);
                String qitaP = result7 + "%";
                byMouthEntity.setQitaP(qitaP);
                qitaSum = 0;
            } else {
                String result7 = numberFormat.format((float) byMouthEntity.getQita() / (float) qitaSum * 100);
                String qitaP = result7 + "%";
                byMouthEntity.setQitaP(qitaP);
            }


            zongSum = huapoSum + bengtaSum + nishiliuSum + dimiantaxianSum + diminachenjiangSum + xiepoSum + qitaSum;
            if (zongSum == 0) {
                zongSum = 1;
                String result8 = numberFormat.format((float) byMouthEntity.getZongshu() / (float) zongSum * 100);
                String zongshuP = result8 + "%";
                byMouthEntity.setZongshuP(zongshuP);
                zongSum = 0;
            } else {
                String result8 = numberFormat.format((float) byMouthEntity.getZongshu() / (float) zongSum * 100);
                String zongshuP = result8 + "%";
                byMouthEntity.setZongshuP(zongshuP);
            }


            List list1 = new ArrayList();
            list1.add(byMouthEntity.getMouth());
            list1.add(byMouthEntity.getZongshu());
            list1.add(byMouthEntity.getZongshuP());
            list1.add(byMouthEntity.getHuapo());
            list1.add(byMouthEntity.getHuapoP());
            list1.add(byMouthEntity.getBengta());
            list1.add(byMouthEntity.getBengtaP());
            list1.add(byMouthEntity.getNishiliu());
            list1.add(byMouthEntity.getNishiliuP());
            list1.add(byMouthEntity.getDimiantaxian());
            list1.add(byMouthEntity.getDimiantaxianP());
            list1.add(byMouthEntity.getDimianchenjiang());
            list1.add(byMouthEntity.getDimianchenjiangP());
            list1.add(byMouthEntity.getXiepo());
            list1.add(byMouthEntity.getXiepoP());
            list1.add(byMouthEntity.getQita());
            list1.add(byMouthEntity.getQitaP());

            lists.add(list1);
        }
        List list2 = new ArrayList();
        list2.add("合计");
        list2.add(zongSum);
        if (zongSum == 0) {
            list2.add("0%");
        } else {
            list2.add("100%");
        }
        list2.add(huapoSum);
        if (huapoSum == 0) {
            list2.add("0%");
        } else {
            list2.add("100%");
        }

        list2.add(bengtaSum);
        if (bengtaSum == 0) {
            list2.add("0%");
        } else {
            list2.add("100%");
        }

        list2.add(nishiliuSum);
        if (nishiliuSum == 0) {
            list2.add("0%");
        } else {
            list2.add("100%");
        }

        list2.add(dimiantaxianSum);
        if (dimiantaxianSum == 0) {
            list2.add("0%");
        } else {
            list2.add("100%");
        }

        list2.add(diminachenjiangSum);
        if (diminachenjiangSum == 0) {
            list2.add("0%");
        } else {
            list2.add("100%");
        }

        list2.add(xiepoSum);
        if (xiepoSum == 0) {
            list2.add("0%");
        } else {
            list2.add("100%");
        }
        list2.add(qitaSum);
        if (qitaSum == 0) {
            list2.add("0%");
        } else {
            list2.add("100%");
        }
        lists.add(list2);


        Map<String, Object> map = new HashMap<>();
        map.put("lists", lists);
        map.put("mouth", mouthList);
        map.put("sum", countList);

        return map;
    }

    @Override
    public Map<String, Object> getExcelByArea(String id) throws Exception {
        AttachmentInfoEntity attach = attachmentInfoService.getAttach(id);
        File file = new File(attach.getFileDirId() + "\\" + attach.getAttachPath());
        // File file=new File("D:\\static\\2022\\09\\21\\区域统计模板_1663741570122.xlsx");
        FileInputStream fileInputStream = new FileInputStream(file);
        List<AreaExcelEntity> areaExcelEntities = ExcelReaderUtil.readExcel2Bean(fileInputStream, AreaExcelEntity.class, 1);


        Integer count = 0;
        List<List> lists = new ArrayList<>();
        int countSum = 0;
        int huapoSum = 0;
        int bengtaSum = 0;
        int nishiliuSum = 0;
        int dimiantaxianSum = 0;
        int dimianchenjiangSum = 0;
        int xiepoSum = 0;
        int qitaSum = 0;

        List<Integer> zongshuList = new ArrayList<>();
        List<Integer> huapoList = new ArrayList<>();
        List<Integer> bengtaList = new ArrayList<>();
        List<Integer> nishiliuList = new ArrayList<>();
        List<Integer> dimiantaxianList = new ArrayList<>();
        List<Integer> dimianchenjiangList = new ArrayList<>();
        List<Integer> xiepoList = new ArrayList<>();
        List<Integer> qitaList = new ArrayList<>();


        List<String> areaList = new ArrayList<>();
        List<Integer> countList = new ArrayList<>();
        for (AreaExcelEntity areaExcelEntity : areaExcelEntities) {
            areaList.add(areaExcelEntity.getEbaPrefecture());
            if (areaExcelEntity.getEbaLandslide() == null) {
                areaExcelEntity.setEbaLandslide(0);
            }
            if (areaExcelEntity.getEbaCollapse() == null) {
                areaExcelEntity.setEbaCollapse(0);
            }
            if (areaExcelEntity.getEbaDebrisFlow() == null) {
                areaExcelEntity.setEbaDebrisFlow(0);
            }
            if (areaExcelEntity.getEbaSurfaceCollapse() == null) {
                areaExcelEntity.setEbaSurfaceCollapse(0);
            }
            if (areaExcelEntity.getEbaLandSubsidence() == null) {
                areaExcelEntity.setEbaLandSubsidence(0);
            }
            if (areaExcelEntity.getEbaUnstableSlope() == null) {
                areaExcelEntity.setEbaUnstableSlope(0);
            }
            if (areaExcelEntity.getEbaOther() == null) {
                areaExcelEntity.setEbaOther(0);
            }
            count = areaExcelEntity.getEbaLandslide() + areaExcelEntity.getEbaCollapse() + areaExcelEntity.getEbaDebrisFlow() + areaExcelEntity.getEbaSurfaceCollapse() + areaExcelEntity.getEbaLandSubsidence()
                    + areaExcelEntity.getEbaUnstableSlope() + areaExcelEntity.getEbaOther();
            countList.add(count);
            areaExcelEntity.setEbaCount(count);
            List list = new ArrayList();
            list.add(areaExcelEntity.getEbaPrefecture());
            list.add(areaExcelEntity.getEbaCount());
            list.add(areaExcelEntity.getEbaLandslide());
            list.add(areaExcelEntity.getEbaCollapse());
            list.add(areaExcelEntity.getEbaDebrisFlow());
            list.add(areaExcelEntity.getEbaSurfaceCollapse());
            list.add(areaExcelEntity.getEbaLandSubsidence());
            list.add(areaExcelEntity.getEbaUnstableSlope());
            list.add(areaExcelEntity.getEbaOther());
            lists.add(list);

            zongshuList.add(count);
            huapoList.add(areaExcelEntity.getEbaLandslide());
            bengtaList.add(areaExcelEntity.getEbaCollapse());
            nishiliuList.add(areaExcelEntity.getEbaDebrisFlow());
            dimiantaxianList.add(areaExcelEntity.getEbaSurfaceCollapse());
            dimianchenjiangList.add(areaExcelEntity.getEbaLandSubsidence());
            xiepoList.add(areaExcelEntity.getEbaUnstableSlope());
            qitaList.add(areaExcelEntity.getEbaOther());
        }


        for (Integer integer : huapoList) {
            huapoSum += integer;
        }

        for (Integer integer : bengtaList) {
            bengtaSum += integer;
        }
        for (Integer integer : nishiliuList) {
            nishiliuSum += integer;
        }
        for (Integer integer : dimiantaxianList) {
            dimiantaxianSum += integer;
        }
        for (Integer integer : dimianchenjiangList) {
            dimianchenjiangSum += integer;
        }
        for (Integer integer : xiepoList) {
            xiepoSum += integer;
        }
        for (Integer integer : qitaList) {
            qitaSum += integer;
        }
        countSum = huapoSum + bengtaSum + nishiliuSum + dimiantaxianSum + dimianchenjiangSum + xiepoSum + qitaSum;


        List list2 = new ArrayList();
        list2.add("合计");
        list2.add(countSum);
        list2.add(huapoSum);
        list2.add(bengtaSum);
        list2.add(nishiliuSum);
        list2.add(dimiantaxianSum);
        list2.add(dimianchenjiangSum);
        list2.add(xiepoSum);
        list2.add(qitaSum);
        lists.add(list2);
        //每个区域的总数
        //区域的集合


        Map<String, Object> map = new HashMap<>();
        map.put("lists", lists);
        map.put("areaList", areaList);
        map.put("countList", countList);
        return map;
    }


    /*
    *
    * 查询最终成果
    * */
    @Override
    public List<SecondDisastersEntity> getAllSecondDisastersTwo(String id) {
        List<SecondDisastersEntity> allSecondDisastersTwo = secondDisastersRepository.getAllSecondDisastersTwo(id);
        for (SecondDisastersEntity secondDisastersEntity : allSecondDisastersTwo) {
            if (!PlatformObjectUtils.isEmpty(secondDisastersEntity.getSsdDescribe())){
                List<String> list = dataString(secondDisastersEntity.getSsdDescribe());
                secondDisastersEntity.setDescribe(list);
            }
        }



        return allSecondDisastersTwo;
    }

    @Override
    public void firstSubmitSecondDisasters(String id) {
        String userID = PlatformSessionContext.getUserID();
        Timestamp currentTimestamp = PlatformDateUtils.getCurrentTimestamp();
        SecondDisastersEntity secondDisastersById = secondDisastersRepository.getSecondDisastersById(id);
        List<SecondDisastersEntity> secondDisastersEntities = secondDisastersRepository.getSecondDisastersByContent(secondDisastersById.getSsdContent(), secondDisastersById.getSsdRelation());


        if (secondDisastersEntities != null) {

            if (secondDisastersEntities.get(0).getSsdDescribe() != "") {
                secondDisastersById.setSsdDescribe(secondDisastersEntities.get(0).getSsdDescribe());
                secondDisastersRepository.firstSubmitSecondDisasters(id, userID, currentTimestamp, secondDisastersById.getSsdDescribe());
            } else {
                secondDisastersRepository.firstSubmitSecondDisasters2(id, userID, currentTimestamp);
            }
        } else {
            secondDisastersRepository.firstSubmitSecondDisasters2(id, userID, currentTimestamp);
        }
    }

    @Override
    public Boolean SecondDisastersIfRepetition(SecondDisastersEntity secondDisastersEntity) {
        if ("地质灾害统计".equals(secondDisastersEntity.getSsdContent())){
            if (PlatformObjectUtils.isNotEmpty(secondDisastersEntity.getSsdMonthTitle())){
                SecondDisastersEntity entity1 = secondDisastersRepository.check1(secondDisastersEntity);
                if (entity1 == null) {
                    return false;
                } else {
                    return true;
                }
            }
            if (PlatformObjectUtils.isNotEmpty(secondDisastersEntity.getSsdAreaTitle())){
                SecondDisastersEntity entity2 = secondDisastersRepository.check2(secondDisastersEntity);
                if (entity2 == null) {
                    return false;
                } else {
                    return true;
                }
            }
        }
        SecondDisastersEntity secondDisastersEntity1 = secondDisastersRepository.SecondDisastersIfRepetition(secondDisastersEntity);
        if (secondDisastersEntity1 == null) {
            return false;
        } else {
            return true;
        }
    }

    @Override
    public void addSecondDisastersDisaster(List<String> ids, String describe) {
        secondDisastersRepository.addSecondDisastersDisaster(ids, describe);
    }

    @Override
    public void removeSecondDisasters(String id) {
        SecondDisastersEntity secondDisastersById = secondDisastersRepository.getSecondDisastersById(id);
        secondDisastersById.setMark(YNEnum.N.toString());
        secondDisastersById.setSsdDescribe("");
        secondDisastersById.setSubmiter("");
        secondDisastersById.setSubmitPhone("");
        secondDisastersRepository.removeSecondDisasters(secondDisastersById);
    }

    @Override
    public void secondSubmitSecondDisasters(List<String> ids) {
        String userJson = sUserService.getSUser(PlatformSessionUtils.getUserId());
        SUser sUser = CreateGson.createGson().fromJson(userJson, SUser.class);
        Timestamp currentTimestamp = PlatformDateUtils.getCurrentTimestamp();
        secondDisastersRepository.secondSubmitSecondDisasters(ids, sUser.getMobilePhone(), sUser.getUserName(), currentTimestamp);

    }

    @Override
    public List<SecondDisastersEntity> getSecondDisastersByIaId(String iaId) {
        return secondDisastersRepository.getSecondDisastersByIaaId(iaId);
    }

    /*
     * 字符串转集合方法
     * */
    private List<String> dataString(String sdaAssFileDescribe) {
        String[] split = sdaAssFileDescribe.split("@@@");
        List<String> disList = new ArrayList<>();
        Collections.addAll(disList, split);
        return disList;
    }




    private String getImageFileId(MultipartFile image)throws IOException {
        //获取文件名称
        String fileName=image.getOriginalFilename();
        //获取文件上传的本地路径
        String dateDir = new SimpleDateFormat("/yyyy/MM/dd").format(new Date());
        String savePath = localDir + dateDir;
        //构建文件
        assert fileName != null;
        //上传文件
        String realFileName = uploadFile(fileName,savePath,image);
        //构建实体类
        AttachmentInfoEntity attachmentInfoEntity = buildAttachment(fileName,dateDir,realFileName);
        List<AttachmentInfoEntity> list=new ArrayList<>();
        list.add(attachmentInfoEntity);
        attachmentInfoService.save(list);
        return attachmentInfoEntity.getAttachId();
    }

    private AttachmentInfoEntity buildAttachment(String fileName, String dateDir, String realFileName) {
        AttachmentInfoEntity attachmentInfoEntity = new AttachmentInfoEntity();
        attachmentInfoEntity.setAttachId(StringUtils.buildPrimaryId());
        attachmentInfoEntity.setAttachName(fileName);
        attachmentInfoEntity.setAttachPath(dateDir + "/" + realFileName);
        attachmentInfoEntity.setFileDirId(localDir);
        attachmentInfoEntity.setIsValid("1");
        attachmentInfoEntity.setIsCompleted("1");
        return attachmentInfoEntity;
    }

    private String uploadFile(String fileName, String savePath, MultipartFile multipartFile) throws IOException {
        // 获取文件的后缀名
        assert fileName != null;
        String preFileName = fileName.substring(0, fileName.lastIndexOf("."));
        // .jpg  .pdf  .txt  .docs
        String fileType = fileName.substring(fileName.lastIndexOf("."));
        // 拼文件名  真实的文件名称
        String realFileName = preFileName + "_" + System.currentTimeMillis() + fileType;
        //判断文件夹是否存在如果不存在则创建
        if (!new File(savePath).exists()) {
            new File(savePath).mkdirs();
        }
        File file = new File(savePath, realFileName);
        multipartFile.transferTo(file);
        return realFileName;
    }


}
